import requests
import json


topic_ids = [37897, 37896, 37895, 37894, 37893, 37892, 37891, 37890, 37889]

# 第一个接口：获取主题列表
url_get_topics = f"http://localhost:8080/api/ccSmart/getTopicListByIds/invoke?ids={','.join(map(str, topic_ids))}"

response = requests.request("GET", url_get_topics)
print("第一个接口响应:", response.text)

# 解析第一个接口的响应，提取data.data
try:
    response_json = response.json()
    if response_json.get("success") and "data" in response_json and "data" in response_json["data"]:
        topics_data = response_json["data"]["data"]
    else:
        topics_data = []
        print("警告：第一个接口返回的数据格式不符合预期")
except json.JSONDecodeError:
    topics_data = []
    print("错误：第一个接口返回的不是有效的JSON")

# 第二个接口：保存或更新主题
url_save_update = "http://106.52.201.90:7001/api/commonDml/saveOrUpdate/invoke"

payload = json.dumps({
    "ds": "cc_media_source",
    "entityClass": "com.coocaa.media.sync.repository.business.entity.CcSmartTopicEntity",
    "serviceClass": "com.coocaa.media.sync.repository.business.service.CcSmartTopicService",
    "data": topics_data  # 使用从第一个接口提取的数据
})
headers = {
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
    'Content-Type': 'application/json'
}

response = requests.request("POST", url_save_update, headers=headers, data=payload)
print("第二个接口响应:", response.text)